package net.jxtz.jxtzos.service.spider.agentsupermarket;


import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.jxtz.jxtzos.entity.spider.agentsupermarket.indexpage.IndexPageHttpPostEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author a123
 */

@Service
@Slf4j
public class MyHttpPost {

    private final static CloseableHttpClient CLIENTS = HttpClients.createDefault();

    // 三秒钟
    private final static Integer SLEEP_SECOND_TIME = 3;

    @SneakyThrows
    public String indexClient(String url, IndexPageHttpPostEntity requestBody) {
        TimeUnit.SECONDS.sleep(SLEEP_SECOND_TIME);
        HttpPost post = new HttpPost(url);
        post.setHeader("Host", "jxswszjcs.jxzwfww.gov.cn");
        post.setHeader("Origin", "https://jxswszjcs.jxzwfww.gov.cn");
        post.setHeader("Referer", "https://jxswszjcs.jxzwfww.gov.cn/jx-zjcs-qt/bidResultNotice/list");
        post.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36");
        post.setHeader("Content-Type", "application/json;charset=UTF-8");
        post.setHeader("Sec-Fetch-Mode", "cors");
        post.setHeader("sec-ch-ua", "\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"92\"");
        post.setEntity(new StringEntity(JSON.toJSONString(requestBody), "utf-8"));
        CloseableHttpResponse response = CLIENTS.execute(post);
        return EntityUtils.toString(response.getEntity());
    }


    @SneakyThrows
    public String detailsClient(String projectCode) {
        TimeUnit.SECONDS.sleep(SLEEP_SECOND_TIME);
        String url = "https://jxswszjcs.jxzwfww.gov.cn/jx-zjcs-pub/api/bidResultNotice/view/" + projectCode + "?_t=" + (int) (System.currentTimeMillis() / 1000) + "000";
        HttpGet get = new HttpGet(url);
        get.setHeader("Accept", "application/json, text/plain, */*");
        get.setHeader("Accept-Language", "zh-CN,zh;q=0.9");
        get.setHeader("Host", "jxswszjcs.jxzwfww.gov.cn");
        get.setHeader("Referer", "https://jxswszjcs.jxzwfww.gov.cn/jx-zjcs-qt/bidResultNotice/view/" + projectCode);
        get.setHeader("sec-ch-ua", "\"Chromium\";v=\"92\", \" Not A;Brand\";v=\"99\", \"Google Chrome\";v=\"92\"");
        get.setHeader("Sec-Fetch-Mode", "cors");
        get.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36");
        CloseableHttpResponse response = CLIENTS.execute(get);
        return EntityUtils.toString(response.getEntity());
    }
}
